package com.chazz.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public final class DBUtils {
	// 连接池类的实例
	private static ConnectionPool connectionPool = ConnectionPool.getInstance();
	// 连接对象
	private static Connection connection;

	private DBUtils() {

	}

	/**
	 * 执行查询操作返回第一行第一列的值（唯一值）
	 * @param sql SQL语句
	 * @param params 占位符中参数值的集合
	 * @return 唯一值
	 */
	public static Object executeUniqueObject(String sql,Object... params){
		Object result = null;
		// 获取连接对象
		connection = connectionPool.getConnection();
		try {
			PreparedStatement pstmt = connection.prepareStatement(sql);
			if (params != null&&params[0]!=null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}
			}
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				result = rs.getObject(1);
			}
			rs.close();
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			close();
		}
		return  result;
	}
	/**
	 * 执行DML操作
	 *
	 * @param sql
	 * @param params
	 * @return
	 */
	public static int executeUpdate(String sql, Object... params) {
		int rows = 0;
		// 获取连接对象
		connection = connectionPool.getConnection();
		try {
			PreparedStatement pstmt = connection.prepareStatement(sql);
			if (params != null&&params[0]!=null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}
			}
			rows = pstmt.executeUpdate();
		} catch (SQLException ex) {
			return -1;
			//ex.printStackTrace();
		} finally {
			close();
		}
		System.out.println(sql);
		return rows;
	}

	/**
	 * 执行DQL操作
	 *
	 * @param sql
	 * @param params
	 * @return
	 */
	public static ResultSet executeQuery(String sql, Object... params) {
		ResultSet rs = null;
		// 获取连接对象
		connection = connectionPool.getConnection();
		try {
			PreparedStatement pstmt = connection.prepareStatement(sql);
			if (params != null&&params[0]!=null) {
				for (int i = 0; i < params.length; i++) {
					pstmt.setObject(i + 1, params[i]);
				}
			}
			rs = pstmt.executeQuery();
		} catch (SQLException ex) {
			return null;
			//ex.printStackTrace();
		}
		return rs;
	}

	/**
	 * 释放连接
	 */
	public static void close() {
		// 将连接对象放入池中
		connectionPool.release(connection);
	}
}
